package com.example.zlloggingservice.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zlloggingservice.common.base.PageResponse;
import com.example.zlloggingservice.common.exception.BaseException;
import com.example.zlloggingservice.dto.request.OperationLogReqDTO;
import com.example.zlloggingservice.dto.response.OperationLogResDTO;
import com.example.zlloggingservice.entity.OperationLog;
import com.example.zlloggingservice.mapper.OperationLogMapper;
import com.example.zlloggingservice.service.IOperationLogService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author zhenliu（孙凌岳）
 * @Description TODO
 * @Date 2025/6/20 16:50
 * @Version 1.0
 */
@Service
public class OperationLogServiceImpl extends ServiceImpl<OperationLogMapper, OperationLog> implements IOperationLogService {
    @Resource
    private OperationLogMapper operationLogMapper;

    @Override
    public PageResponse<OperationLogResDTO> findOperationResDTOPage(OperationLogReqDTO operationLogReqDTO, int pageNum, int pageSize) {
        QueryWrapper<OperationLog> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(operationLogReqDTO.getAction())) {
            queryWrapper.like("action", operationLogReqDTO.getAction());
        }

        if (ObjectUtil.isNotEmpty(operationLogReqDTO.getUsername())) {
            queryWrapper.eq("username", operationLogReqDTO.getUsername());
        }

        if (ObjectUtil.isNotEmpty(operationLogReqDTO.getStatus())) {
            queryWrapper.eq("status", operationLogReqDTO.getStatus());
        }

        //降序排列
        queryWrapper.orderByDesc("oper_time");

        //构造分页对象
        Page<OperationLog> page = new Page<>(pageNum, pageSize);
        //查询信息
        operationLogMapper.selectPage(page, queryWrapper);
        //封装信息
        return PageResponse.of(page,OperationLogResDTO.class);
    }

    @Override
    public Integer deleteLogById(Long operId){
            //删除用户信息
            int delete = operationLogMapper.deleteById(operId);
            if (delete == 0) {
                throw new BaseException("删除日志失败");
            }
            return delete;
    }
}
